/**
 * Created by lins on 2018-7-12.
 */
angular.module('MetronicApp').controller('InformationListController',
    ['$rootScope', '$scope', '$state', '$uibModal', '$stateParams', 'toastr', 'EnumService', 'InformationService',
        function ($rootScope, $scope, $state, $uibModal, $stateParams, toastr, EnumService, InformationService) {
            $scope.$on('$viewContentLoaded', function () {
                App.initAjax();
                $rootScope.settings.layout.pageBodySolid = true;
                $rootScope.settings.layout.pageSidebarClosed = false;
            });
            $scope.tabItemList = [
                {
                    "name": "标签体系",
                    "router": "labelSystemList",
                    "selected": false
                },
                {
                    "name": "企业信息",
                    "router": "informationList",
                    "selected": true
                },
                {
                    "name": "挖掘算法",
                    "router": "algorithmList",
                    "selected": false
                },
                {
                    "name": "企业画像",
                    "router": "businessPortraitList",
                    "selected": false
                },
                {
                    "name": "标签检索",
                    "router": "labelRetrievalList",
                    "selected": false
                }
            ]

            $scope.condition = {};
            $scope.range = EnumService.get("area");

            //获取ng-table表头
            $scope.columns = InformationService.getSchema();
            $scope.pageable = InformationService.getPageable();
            $scope.$watch('pageable.size', function (newVal, oldVal) {
                if (newVal === oldVal) return;
                InformationService.setSize(newVal);
                $scope.list();
            });
            $scope.$watch('pageable.number', function (newVal, oldVal) {
                if (newVal === oldVal) return;
                InformationService.setStoredPage(newVal);
                $scope.list();
            });

            $scope.list = function () {
                InformationService.putSearchParams({
                    search: $scope.condition.search
                });
                InformationService.list(function (res) {
                    if ('success' === res.status) {
                        $scope.rows = res.data;
                        $scope.pageable = res.pageable;
                    } else {
                        for (var i = 0; i < res.errors.length; i++) {
                            toastr.error(res.errors[i].errmsg, "数据获取失败");
                        }
                    }
                });
            };

            var gotoFirstPage = function () {
                InformationService.setStoredPage(0);
                $scope.list();
                InformationService.clearSearchParams();
            };
            gotoFirstPage();

            //查询
            $scope.search = function () {
                gotoFirstPage();
            };

            //编辑
            $scope.edit = function (row) {
                $state.go('informationView', {id: row.id});
            };

            //详情
            $scope.view = function (row) {
                $state.go('informationView', {id: row.id});
            };
            //标签
            $scope.label = function () {
                $state.go('informationLabel', {id: row.id});
            };

        }
    ]
);